package com.abc.service.impl;

import com.abc.dto.request.ApiRequestRequestDto;
import com.abc.dto.request.Message;
import com.abc.dto.response.SseResponse;
import com.abc.service.ChatGPTService;
import com.abc.utils.SparkRequestUtil;
import com.abc.utils.SparkRequestWebSocketUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatGPTServiceImpl implements ChatGPTService {
    @Autowired
    private SparkRequestUtil sparkRequestUtil;
    @Autowired
    private SparkRequestWebSocketUtil sparkRequestWebSocketUtil;

    @Override
    public void chat(String input, SseEmitter emitter) {
        ApiRequestRequestDto requestDto = getApiRequestRequestDto(input);
        sparkRequestUtil.sendApiStreamRequest(requestDto, emitter);
    }

    @Override
    public void chatWebSocket(String input, WebSocketSession session) {
        ApiRequestRequestDto requestDto = getApiRequestRequestDto(input);
        sparkRequestWebSocketUtil.sendApiStreamRequest(requestDto, session);
    }

    @NotNull
    private static ApiRequestRequestDto getApiRequestRequestDto(String input) {
        ApiRequestRequestDto requestDto = new ApiRequestRequestDto();
        List<Message> messageList = new ArrayList<>();
        Message message = new Message();
        message.setContent(input);
        messageList.add(message);
        requestDto.setMessages(messageList);
        return requestDto;
    }

}
